<script  setup>
import { ref, defineProps, reactive, defineEmits } from 'vue'
import { onClickOutside } from '@vueuse/core'
import axios from 'axios'
import { computed } from '@vue/reactivity'

defineProps({
  fullLocation: {
    type: String,
    default: ''
  }
})

const loading = ref(false)
const cityList = ref([])
const getCityFn = () => {
  return axios({
    url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json'
  }).then(res => {
    // console.log(res)
    cityList.value = res.data
    window.cityList = res.data
    loading.value = false
  })
}

const visible = ref(false)
const open = () => {
  visible.value = true
  if (window.cityList) {
    cityList.value = window.cityList
  } else {
    loading.value = true
    getCityFn()
  }
}

const close = () => {
  visible.value = false
  for (const key in selectResult) {
    selectResult[key] = ''
  }
}
const toggleFn = () => {
  if (visible.value) {
    close()
  } else {
    open()
  }
}

const target = ref(null)
onClickOutside(target, () => {
  close()
})

const selectResult = reactive({
  provinceCode: '',
  provinceName: '',
  cityCode: '',
  cityName: '',
  countyCode: '',
  countyName: '',
  fullLocation: ''
})

const emit = defineEmits(['selected'])
const ChangeItem = (item) => {
  if (item.level === 0) {
    selectResult.provinceCode = item.code
    selectResult.provinceName = item.name
  }
  if (item.level === 1) {
    selectResult.cityCode = item.code
    selectResult.cityName = item.name
  }
  if (item.level === 2) {
    selectResult.countyCode = item.code
    selectResult.countyName = item.name
    selectResult.fullLocation = selectResult.provinceName + selectResult.cityName + selectResult.countyName
    emit('selected', selectResult)
    close()
  }
}

const city = computed(() => {
  let province = {}
  let result = cityList.value
  if (selectResult.provinceCode) {
    province = cityList.value.find(item => item.code === selectResult.provinceCode)
    result = province.areaList
  }

  if (selectResult.cityCode) {
    province = province.areaList.find(item => item.code === selectResult.cityCode)
    result = province.areaList
  }
  return result
})
</script>
<template>
<div class="xtx-city" ref="target">
  <div class="select" @click="toggleFn" :class="{active:visible}">
    <!-- <span class="value">{{ selectResult }}</span> -->
    <span class="placeholder" >请选择配送地址</span>
    <span class="value">{{ fullLocation }}</span>
    <i class="iconfont icon-angle-down"></i>
  </div>
  <div class="option" v-show="visible">
    <div class="loading" v-if="loading"></div>
    <template v-else>
      <span class="ellipsis" v-for="item in city" :key="item"  @click="ChangeItem(item)">
        {{ item.name }}
    </span>
    </template>

  </div>
  </div>
</template>

<style scoped lang="less">
  .xtx-city {
    display: inline-block;
    position: relative;
    z-index: 400;
    .select {
      border: 1px solid #e4e4e4;
      height: 30px;
      padding: 0 5px;
      line-height: 28px;
      cursor: pointer;
      &.active {
        background: #fff;
      }
      .placeholder {
        color: #999;
      }
      .value {
        color: #666;
        font-size: 12px;
      }
      i {
        font-size: 12px;
        margin-left: 5px;
      }
    }
    .option {
      width: 542px;
      border: 1px solid #e4e4e4;
      position: absolute;
      left: 0;
      top: 29px;
      background: #fff;
      min-height: 30px;
      line-height: 30px;
      display: flex;
      flex-wrap: wrap;
      padding: 10px;
      > span {
        width: 130px;
        text-align: center;
        cursor: pointer;
        border-radius: 4px;
        padding: 0 3px;
        &:hover {
          background: #f5f5f5;
        }
      }
    }
  }
  .loading{
    height: 290px;
    width: 100%;
    background: url('../assets/images/loading.gif') no-repeat center;
  }
</style>
